﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace PRO_DOCS.DocumentManagementSystem.ADMIN.WIN.Configurations.Storage
{
    public partial class StoragePhysicalPathes : BLL.WIN.Templates.DevxConfigurationUserControlTemplate
    {
        private DAL.Database.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHE _aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH;
        public DAL.Database.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHE SELECTED_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH
        {
            get { return _aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH; }
            set
            {
                _aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH = value;

                if (_aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH_ID > 0)
                {
                    txtPathName.Text = _aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH_NAME;
                    txtSavingPathDirectory.Text = _aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH;
                    comboPathStatus.EditValue = _aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH_STATUSE.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH_STATUS_NAME;
                }
            }
        }


        private List<DAL.Database.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHE> _aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHES;
        public List<DAL.Database.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHE> SELECTED_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHES
        {
            get { return _aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHES; }
            set
            {
                _aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHES = value;
                RELOAD_SELECTED_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHES();

            }
        }

        private void RELOAD_SELECTED_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHES()
        {
            SELECTED_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH = new DAL.Database.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHE();

            gridControl1.DataSource = SELECTED_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHES;

            gridView1.ExpandAllGroups();
        }



        private DAL.Database.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHy _sELECTED_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY;
        public DAL.Database.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHy SELECTED_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY
        {
            get { return _sELECTED_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY; }
            set
            {
                CLEAR_STORAGE_PHYSICAL_PATHES();
                _sELECTED_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY = value;

                if (_sELECTED_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY.ARCHIVE_DEPARTMENT_STORAGE_HEIRARCHY_ID > 0)
                {

                    SELECTED_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHES = (from pathes in PRO_DOCSDatabaseDataContext.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHEs
                                                                                       where pathes.ARCHIVE_DEPARTMENT_STORAGE_HEIRARCHY_ID.Value.Equals(_sELECTED_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY.ARCHIVE_DEPARTMENT_STORAGE_HEIRARCHY_ID)
                                                                                       select pathes).ToList();


                }
            }
        }

        Dictionary<int, DAL.Database.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH_STATUSE> ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH_STATUSES
        { get; set; }

        public StoragePhysicalPathes()
        {
            InitializeComponent();

            ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH_STATUSES = new Dictionary<int, DAL.Database.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH_STATUSE>();

            List<DAL.Database.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH_STATUSE> aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH_STATUSES = PRO_DOCSDatabaseDataContext.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH_STATUSEs.ToList();

            comboPathStatus.Properties.Items.BeginUpdate();
            comboPathStatus.Properties.Items.Clear();

            for (int i = 0; i < aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH_STATUSES.Count; i++)
            {
                ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH_STATUSES.Add(i, aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH_STATUSES[i]);
                comboPathStatus.Properties.Items.Add(aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH_STATUSES[i].ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH_STATUS_NAME);
            }

            comboPathStatus.Properties.Items.EndUpdate();
            comboPathStatus.SelectedIndex = -1;


            btnAddNew.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btnAddNew_ItemClick);
            btnDeleteItem.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btnDeleteItem_ItemClick);
            btnRefresh.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btnRefresh_ItemClick);
            btnSaveUpdate.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btnSaveUpdate_ItemClick);

        }

        void btnSaveUpdate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            dxErrorProvider1.ClearErrors();
            bool hasStorageName = !string.IsNullOrEmpty(txtPathName.Text);
            bool hasStoragePath = !string.IsNullOrEmpty(txtSavingPathDirectory.Text);
            bool hasStatus = comboPathStatus.SelectedIndex > -1;

            if (hasStorageName && hasStoragePath && hasStatus)
            {

                int? aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH_ID = 0;

                // = (SELECTED_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH != null) ? SELECTED_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH : new DAL.Database.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHE();
                if (SELECTED_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH_ID > 0) //update
                {
                    aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH_ID = SELECTED_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH_ID;
                    PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHESUpdate
                        (
                            aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH_ID,
                            SELECTED_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY.ARCHIVE_DEPARTMENT_STORAGE_HEIRARCHY_ID,
                            txtPathName.Text,
                            txtSavingPathDirectory.Text,
                            ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH_STATUSES[comboPathStatus.SelectedIndex].ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH_STATUS_ID
                        );

                    gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gcPathName, txtPathName.Text);
                    gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gcPhysicalPath, txtSavingPathDirectory.Text);
                    gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gcFolderLevel, comboPathStatus.Text);
                }
                else
                {
                    PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHESInsert
                        (
                            ref aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH_ID,
                            SELECTED_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY.ARCHIVE_DEPARTMENT_STORAGE_HEIRARCHY_ID,
                            txtPathName.Text,
                            txtSavingPathDirectory.Text,
                            ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH_STATUSES[comboPathStatus.SelectedIndex].ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH_STATUS_ID
                        );

                }

                SELECTED_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY = (from storage in PRO_DOCSDatabaseDataContext.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHies
                                                                   where storage.ARCHIVE_DEPARTMENT_STORAGE_HEIRARCHY_ID.Equals(SELECTED_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY.ARCHIVE_DEPARTMENT_STORAGE_HEIRARCHY_ID)
                                                                   select storage).FirstOrDefault();

                //RELOAD_SELECTED_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHES();
            }
            else
            {
                if (!hasStorageName)
                {
                    dxErrorProvider1.SetError(txtPathName, BLL.WIN.Properties.ErrorProviderMessages.Default.InputLeftBlank.Replace("{name}", "Path name"));
                }
                if (!hasStoragePath)
                {
                    dxErrorProvider1.SetError(txtSavingPathDirectory, BLL.WIN.Properties.ErrorProviderMessages.Default.InputLeftBlank.Replace("{name}", "Physical path"));
                }
                if (!hasStatus)
                {
                    dxErrorProvider1.SetError(comboPathStatus, BLL.WIN.Properties.ErrorProviderMessages.Default.InputLeftBlank.Replace("{name}", "Path status"));
                }
            }
        }

        void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RELOAD_SELECTED_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHES();
        }

        void btnDeleteItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (SELECTED_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH_ID > 0)
            {
                try
                {
                    PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHESDelete
                        (SELECTED_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH_ID);

                    gridView1.DeleteRow(gridView1.FocusedRowHandle);
                }
                catch
                {
                    XtraMessageBox.Show("The Storage path cannot be deleted as it is already used in another transaction(s)", "Action denied", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }

            }
        }

        void btnAddNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SELECTED_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH = new DAL.Database.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHE();
            CLEAR_STORAGE_PHYSICAL_PATHES();
        }


        internal void CLEAR_STORAGE_PHYSICAL_PATHES()
        {
            txtPathName.Text = string.Empty;
            txtSavingPathDirectory.Text = string.Empty;
            comboPathStatus.SelectedIndex = -1;
        }

        private void btnOpenFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (!string.IsNullOrEmpty(txtSavingPathDirectory.Text))
            {
                fbd.SelectedPath = txtPathName.Text;
            }
            fbd.ShowNewFolderButton = true;
            fbd.Description = "Select storage destination folder.";

            if (fbd.ShowDialog().Equals(DialogResult.OK))
            {
                txtSavingPathDirectory.Text = fbd.SelectedPath;
            }
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (e.FocusedRowHandle > -1)
            {

                SELECTED_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH = gridView1.GetRow(e.FocusedRowHandle) as DAL.Database.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHE;
            }
        }
    }
}
